package com.tmxk.api.days03.zjq;

import java.util.*;

/**
 * @BelongsProject: java-api1
 * @BelongsPackage: com.tmxk.api.days03.zjq
 * @Author: zjq
 * @CreateTime: 2022-11-30  09:52
 * @Description: TODO
 * @Version: 1.0
 */
public class StringDemo {
    static Scanner sc = new Scanner(System.in);
    public static Map<String,User> userMap =  new HashMap<>();
    static{
        User user1 = new User("1","zz","123","123","123","123");
        User user2 = new User("2","zza","1233","123","123","123");
        userMap.put(user1.getUserId(),user1);
        userMap.put(user2.getUserId(),user2);
    }



    public void userLogin(){
        //登录：用户名，密码
        System.out.println("请登录");
        System.out.print("请输入用户名:");
        String userName = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        Collection<User> users = userMap.values();
        for (User u : users) {
            if (userName.equals(u.getUserName()) && password.equals(u.getPassword(password))) {
                System.out.println("登录成功");
                break;
            } else {
                System.out.println("登录失败,请重新登录");
            }
        }
    }



    public void userRegist() {
        //登录：用户名，密码
        System.out.println("************用户注册************");
        System.out.print("请输入用户名:");
        String um = sc.next();
        System.out.print("请输入密码:");
        String pwd = sc.next();
        //校验密码长度
        if (pwd.length() < 6) {
            System.out.print("注册用户密码不能少于6位，请重新输入用户密码:");
            pwd = sc.next();
        }
        System.out.print("请再次确认密码:");
        String confirmePwd = sc.next();
        if (!confirmePwd.equals(pwd)) {
            System.out.print("两次密码不一致，请重新输入确认密码：");
            confirmePwd = sc.next();
        }
        System.out.print("请输入手机号码:");
        String phone = sc.next();
        //定义手机号正则匹配
        String reg = "1[0-9]{10}";
        while (!phone.matches(reg)) {
            System.out.print("手机号必须是11位，请重新输入:");
            phone = sc.next();
        }

        System.out.print("请输入邮箱：");
        String email = sc.next();
        while (email.indexOf("@") == -1 && email.indexOf(".") == -1) {
            System.out.print("邮箱必须包含@和.  请重新输入:");
            email = sc.next();
        }

        System.out.print("请输入身份证:");
        String certificateNum = sc.next();
        String certificateReg = "([0-9]{18}|[0-9]{17}X)";
        while (certificateNum.matches(certificateReg)) {
            System.out.print("身份证输入错误,请重新输入:");
            certificateNum = sc.next();
        }
        System.out.println("注册成功！");

            //将注册成功的用户信息添加到集合中
            User register = new User();
            //UUID国际通用标识符号
            UUID uuid = UUID.randomUUID();
            System.out.println("uuid:");
            register.setUserId(uuid.toString().replace("-", ""));
            register.setUserName(um);
            register.getPassword(pwd);
            register.setPhone(phone);
            register.setEmail(email);
            register.setCertificateNum(certificateReg);
            userMap.put(register.getUserId(),register);
        }

    public static void main(String[] args) {
        StringDemo demo = new StringDemo();
        Scanner sc = new Scanner(System.in);
        for(int o = 0 ; ;o++) {

            System.out.println("欢迎登录");
            try {
                System.out.println("请选择： 1.登录  2.注册  ");
                int i = sc.nextInt();
                if (i == 1) {
                    demo.userLogin();
                } else if (i == 2) {
                    demo.userRegist();
                } else {
                    System.out.println("登录失败，请重新登录");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("程序执行完毕");
            }

        }
    }


}



